package com.focus.xyz.core.common.webdriver;

import com.gargoylesoftware.htmlunit.WebClient;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.WebDriver;

/**
 * Created by chenmeng on 2016/4/19.
 */
public class DriverFactory {
    private final static DriverFactory instance = new DriverFactory();
    private static ThreadLocal<IDriver> driverThreadLocal;

    public DriverFactory() {
        driverThreadLocal = new ThreadLocal<IDriver>();
    }

    public static DriverFactory getInstance() {
        return instance;
    }


    public IDriver getMultiDriver(String nodeUrl){
        IDriver iDriver = null;
        if ((iDriver = driverThreadLocal.get()) == null) {
            if(StringUtils.isBlank(nodeUrl)){
                iDriver = new LocalDriver();
            }else{
                iDriver = new RemoteDriver(nodeUrl);
            }
            driverThreadLocal.set(iDriver);
            System.out.println("为线程 [ " + Thread.currentThread().getName() + " ] 创建新的WebDriver实例");
        } else {
            System.out.println("线程 [ " + Thread.currentThread().getName() + " ] 已有WebDriver实例,直接使用");
        }
        return iDriver;
    }

    public static IDriver getDriver(String nodeUrl){
        if(StringUtils.isBlank(nodeUrl)){
            return new LocalDriver();
        }else{
            return new RemoteDriver(nodeUrl);
        }
    }

    public static IDriver getLocalDriver(){
        return new LocalDriver();
    }

    public static IDriver getRemoteDriver(String nodeUrl){
        return new RemoteDriver(nodeUrl);
    }

}
